//
//  XGUserDetailViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/19.
//  Copyright © 2019 itcast. All rights reserved.
//

#import "XGUserDetailViewController.h"

#import "XGCollectionViewCell.h"

/// cell重用标识符
static NSString *const XGCollectionViewCellReuseIdentifier = @"XGCollectionViewCell";

@interface XGUserDetailViewController ()

/// 帖子列表
@property (nonatomic,strong) NSArray<AVObject *> *topicList;

@end

@implementation XGUserDetailViewController
{
    /// 用户数据模型
    AVUser *_user;
}

#pragma mark - 构造方法

- (instancetype)initWithUserInfo:(AVUser *)user
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    if (self = [super initWithCollectionViewLayout:flowLayout]) {
        _user = user;
    }
    
    return self;
}

+ (instancetype)viewControllerWithUserInfo:(AVUser *)user
{
    return [[self alloc] initWithUserInfo:user];
}

- (instancetype)init
{
    @throw [NSException exceptionWithName:@"创建对象失败" reason:@"请使用指定构造方法 -initWithUserInfo: 或 +viewControllerWithUserInfo: 创建对象" userInfo:nil];
}

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = _user[@"screenName"];
    
    [self setUpCollectionView];
    
    [self loadTopicList];
}

#pragma mark - 数据源和代理方法

// 多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _topicList.count;
}

// 每行的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XGCollectionViewCellReuseIdentifier forIndexPath:indexPath];
    AVObject *topic = _topicList[indexPath.item];
    cell.file = topic[@"pic"];
    return cell;
}

#pragma mark - 其他方法

/// 设置collectionView
- (void)setUpCollectionView
{
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // 布局
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    CGFloat margin = 5;
    CGFloat itemWidth = floor((self.view.width - 4 * margin) / 3);
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    flowLayout.minimumLineSpacing = margin;
    flowLayout.minimumInteritemSpacing = margin;
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    
    // 注册cell
    [self.collectionView registerClass:[XGCollectionViewCell class] forCellWithReuseIdentifier:XGCollectionViewCellReuseIdentifier];
}

/// 加载云端帖子数据
- (void)loadTopicList
{
    AVQuery *query = [[AVQuery alloc] initWithClassName:@"Topic"];
    [query whereKey:@"user" equalTo: _user];
    query.limit = 12;
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (error || objects == nil) {
            DEBUG_Log(@"加载帖子列表失败");
            return;
        }
        
        // 队列组 获取所有帖子里面的user对象数据
        dispatch_group_t group = dispatch_group_create();
        for (AVObject *topic in objects) {
            dispatch_group_enter(group);
            [topic fetchInBackgroundWithKeys:@[@"user"] block:^(AVObject *object, NSError *error) {
                dispatch_group_leave(group);
            }];
        }
        
        // 获取完毕监听 刷新表格
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            self.topicList = objects;
            [self.collectionView reloadData];
        });
    }];
}

- (AVUser *)user
{
    return _user;
}

- (NSArray<AVObject *> *)topicList
{
    return _topicList;
}

@end
